<script setup>
import { Plus } from "@element-plus/icons-vue";
import useTable from "@/utils/useTable";
import {http} from "@/api";
import {computed, ref} from "vue";
const routeList = ref([
  {
    name: '温度数据',
    code: 'temperature-data',
    unit: '℃'
  },
  {
    name: '气压数据',
    code: 'pressure-data',
    unit: 'Pa'
  },
  {
    name: '湿度数据',
    code: 'humidity-data',
    unit: '%'
  },
  {
    name: '辐照度数据',
    code: 'radiation-data',
    unit: 'W/m²'
  },
  {
    name: '总光伏功率数据',
    code: 'total-photovoltaic-power-data',
    unit: 'MV'
  },
  {
    name: '储能无功数据',
    code: 'storage-reactive-power-data',
    unit: 'MVAr'
  },
  {
    name: '储能有功数据',
    code: 'storage-active-power-data',
    unit: 'MW'
  },
  {
    name: '可充电量数据',
    code: 'rechargeable-amount-data',
    unit: 'kWh'
  },
  {
    name: '可放电量数据',
    code: 'dischargeable-amount-data',
    unit: 'kWh'
  },
  {
    name: '电池荷电量数据',
    code: 'battery-charge-amount-data',
    unit: 'kWh'
  }
])
const routeItem = computed(() => {
  excelName.value = routeList.value.find(el => el.code === searchParams.value.type)?.name || ''
  return routeList.value.find(el => el.code === searchParams.value.type)?.unit
})
/**
 * 表格
 * @type {Ref<UnwrapRef<*[]>>}
 */
const {
  searchParams,
  pageNo,
  pageSize,
  total,
  changePage,
  changeSize,
  dataSource,
  search,
  reset,
  exportPage,
  exportAll,
  excelName
} = useTable({
  exportExcelName: '数据查询',
  requestFn: http.dataQuery,
  params: {
    type: 'temperature-data',
    start_time: '',
    end_time: ''
  }
})
</script>


<template>
  <PageMain title="报表-数据查询">
    <el-form label-suffix="：" inline>
      <el-form-item label="模块">
        <el-select style="width: 260px"  v-model="searchParams.type">
          <el-option
              v-for="item in routeList"
              :key="item.code"
              :label="item.name"
              :value="item.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间">
        <el-date-picker
            type="datetime"
            v-model="searchParams.start_time" value-format="YYYY-MM-DD HH:mm:ss" format="YYYY-MM-DD HH:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间">
        <el-date-picker
            type="datetime"
            v-model="searchParams.end_time" value-format="YYYY-MM-DD HH:mm:ss" format="YYYY-MM-DD HH:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="add-box">
      <el-button type="primary" :icon="Plus" @click="exportPage">导出当页</el-button>
      <el-button type="primary" :icon="Plus" @click="exportAll">导出全部</el-button>
    </div>
    <div class="table-box">
      <el-table :data="dataSource">
        <el-table-column label="时间" prop="name">
        </el-table-column>
        <el-table-column label="值" prop="value">
          <template #default="{row}">
            {{ row.value }}{{ routeItem }}
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[10, 50, 100, 200]"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="changeSize"
          @current-change="changePage"
      />
    </div>
  </PageMain>
</template>

<style scoped lang="scss">

</style>
